<?php

/**
 * Description of Security
 *
 * @author G011C1093
 */
class Security {

	private static $stretch_count = 32807;

	private function create_salt() {

		$salt = mt_rand();
		return $salt;
	}

	private function get_sha256($passwd) {

		return $passwd = hash("sha256", $passwd);
	}

	public function salt_protection($pass, $mail) {

		$array["salt"] = " ";
		$array["hash"] = " ";
		$array["mail"] = " ";

		$salt = $this->create_salt();
		$hash_text = $pass;
		
		//salt_add
		$hash_text .= $salt;
		$mail .= $salt;
		//protection_sha256
		for ($i = 0; $i < Security::$stretch_count; $i++) {
			$hash_text = $this->get_sha256($hash_text);
			$mail = $this->get_sha256($mail);
		}
		$array["salt"] = $salt;
		$array["hash"] = $hash_text;
		$array["mail"] = $mail;

		return $array;
	}
	public function salt_pass($pass, $salt) {

		$hash_text = $pass;
		
		//salt_add
		$hash_text .= $salt;
		//protection_sha256
		for ($i = 0; $i < Security::$stretch_count; $i++) {
			$hash_text = $this->get_sha256($hash_text);
		}

		return $hash_text;
	}

	public function password_check($text, $pass) {

		$text .= $this->salt;
		for ($i = 0; $i < Security::$stretch_count; $i++) {
			$text = $this->get_sha256($text);
		}

		if ($pass === $text) {

			return 1;
		} else {

			return 0;
		}
	}

}
?>

